<template>
  <div class="xtx-cart-page">
    <div class="container">
      <BreadCrumbs>
        <XtxBread to="/">首页</XtxBread>
        <XtxBread>购物车</XtxBread>
      </BreadCrumbs>
      <div class="cart">
        <table>
          <thead>
            <tr>
              <th width="120">
                <XtxCheckbox v-model="isSelectedAll" @change="selectAllClick"
                  >全选</XtxCheckbox
                >
              </th>
              <th width="400">商品信息</th>
              <th width="220">单价</th>
              <th width="180">数量</th>
              <th width="180">小计</th>
              <th width="140">操作</th>
            </tr>
          </thead>
          <!-- 有效商品 -->
          <tbody>
            <tr v-for="i in reckonValidList" :key="i.skuId">
              <td>
                <XtxCheckbox
                  v-model="i.selected"
                  @change="(selected) => changeRadio(selected, i.skuId)"
                />
              </td>
              <td>
                <div class="goods">
                  <RouterLink to="/"><img :src="i.picture" /></RouterLink>
                  <div>
                    <p class="name ellipsis">{{ i.name }}</p>
                    <p class="attr">{{ i.attrsText }}</p>
                  </div>
                </div>
              </td>
              <td class="tc">
                <p>&yen;{{ i.nowPrice }}</p>
                <p>
                  比加入时降价
                  <span class="red"
                    >&yen;{{ (i.price - i.nowPrice).toFixed(2) }}</span
                  >
                </p>
              </td>
              <td class="tc">
                <Numbox :max="i.stock" v-model="i.count" @change="(count) => changeCount(count, i.skuId)" />
              </td>
              <td class="tc">
                <p class="f16 red">&yen;{{ (i.nowPrice * i.count).toFixed(2) }}</p>
              </td>
              <td class="tc">
                <p><a href="javascript:;">移入收藏夹</a></p>
                <p><a class="green" href="javascript:;" @click="delGoods(i)">删除</a></p>
                <p><a href="javascript:;">找相似</a></p>
              </td>
            </tr>
          </tbody>
          <!-- 无效商品 -->
          <tbody>
            <tr>
              <td colspan="6"><h3 class="tit">失效商品</h3></td>
            </tr>
            <tr v-for="i in failureValidList" :key="i.skuId">
              <td></td>
              <td>
                <div class="goods">
                  <RouterLink to="/"
                    ><img :src="i.picture" alt=""
                  /></RouterLink>
                  <div>
                    <p class="name ellipsis">{{ i.name }}</p>
                    <p class="attr">{{ i.attrsText }}</p>
                  </div>
                </div>
              </td>
              <td class="tc">
                <p>&yen;{{ i.nowPrice }}</p>
              </td>
              <td class="tc">{{ i.count }}</td>
              <td class="tc">
                <p>&yen;{{ (i.nowPrice * i.count).toFixed(2) }}</p>
              </td>
              <td class="tc">
                <p><a class="green" href="javascript:;">删除</a></p>
                <p><a href="javascript:;">找相似</a></p>
              </td>
            </tr>
          </tbody>
        </table>
      </div>
      <!-- 操作栏 -->
      <div class="action">
        <div class="batch">
          <XtxCheckbox v-model="isSelectedAll" @change="selectAllClick">全选</XtxCheckbox>
          <a href="javascript:;">删除商品</a>
          <a href="javascript:;">移入收藏夹</a>
          <a href="javascript:;">清空失效商品</a>
        </div>
        <div class="total">
          共 {{ reckonListCount }} 件有效商品，已选择
          {{ reckonSelectList.length }} 件，商品合计：
          <span class="red">{{ reckonSelectPrice.toFixed(2) }}</span>
          <Button type="primary" @click="orders">下单结算</Button>
        </div>
      </div>
    </div>
  </div>
</template>
<script>
import { useStore } from 'vuex'
import { computed } from 'vue'
import { useRouter } from 'vue-router'
import { message } from '@/components/Message'

export default {
  name: 'XtxCartPage',
  setup () {
    const store = useStore()
    const router = useRouter()

    // 定义全选按钮状态
    const selectAllClick = (selected) => {
      store.dispatch('cart/selecedAll', selected)
    }

    // 定义单选按钮
    const changeRadio = (selected, skuId) => {
      store.dispatch('cart/changeRadio', { selected, skuId })
    }

    // 删除商品
    const delGoods = (goods) => {
      store.dispatch('cart/delGoods', goods)
    }

    // 商品数量的增加和减少
    const changeCount = (count, skuId) => {
      store.dispatch('cart/changeCount', { count, skuId })
    }

    // 商品数量的增加和减少
    const orders = (count, skuId) => {
      // 1.不能为非登录状态
      if (!store.state.user.profile.token) {
        message({ type: 'warn', text: '请先登录' })
        return false
      }
      // 2. 选择的商品数量不能为零
      if (reckonSelectList.value === 0) {
        message({ type: 'warn', text: '请选择商品下单' })
        return false
      }
      // 跳转到订单结算页
      router.push('/settlement')
    }

    // 计算出有效商品列表
    const reckonValidList = computed(
      () => store.getters['cart/reckonValidList']
    )

    // 计算出有效商品的数量
    const reckonListCount = computed(
      () => store.getters['cart/reckonListCount']
    )

    // 计算出失效的商品列表
    const failureValidList = computed(
      () => store.getters['cart/failureValidList']
    )

    // 计算出有效商品的总价
    const reckonListPrice = computed(
      () => store.getters['cart/reckonListPrice']
    )

    // 计算出选中的商品
    const reckonSelectList = computed(
      () => store.getters['cart/reckonSelectList']
    )

    // 计算出选中的商品
    const reckonSelectPrice = computed(
      () => store.getters['cart/reckonSelectPrice']
    )
    // 计算出是否全选
    const isSelectedAll = computed(() => store.getters['cart/isSelectedAll'])

    return {
      reckonValidList,
      failureValidList,
      reckonListPrice,
      reckonListCount,
      reckonSelectList,
      reckonSelectPrice,
      isSelectedAll,
      changeRadio,
      selectAllClick,
      delGoods,
      changeCount,
      orders
    }
  }
}
</script>
<style scoped lang="less">
.tc {
  text-align: center;
  .xtx-numbox {
    margin: 0 auto;
    width: 120px;
  }
}
.red {
  color: @priceColor;
}
.green {
  color: @xtxColor;
}
.f16 {
  font-size: 16px;
}
.goods {
  display: flex;
  align-items: center;
  img {
    width: 100px;
    height: 100px;
  }
  > div {
    width: 280px;
    font-size: 16px;
    padding-left: 10px;
    .attr {
      font-size: 14px;
      color: #999;
    }
  }
}
.action {
  display: flex;
  background: #fff;
  margin-top: 20px;
  height: 80px;
  align-items: center;
  font-size: 16px;
  justify-content: space-between;
  padding: 0 30px;
  .xtx-checkbox {
    color: #999;
  }
  .batch {
    a {
      margin-left: 20px;
    }
  }
  .red {
    font-size: 18px;
    margin-right: 20px;
    font-weight: bold;
  }
}
.tit {
  color: #666;
  font-size: 16px;
  font-weight: normal;
  line-height: 50px;
}
.xtx-cart-page {
  .cart {
    background: #fff;
    color: #666;
    table {
      border-spacing: 0;
      border-collapse: collapse;
      line-height: 24px;
      th,
      td {
        padding: 10px;
        border-bottom: 1px solid #f5f5f5;
        &:first-child {
          text-align: left;
          padding-left: 30px;
          color: #999;
        }
      }
      th {
        font-size: 16px;
        font-weight: normal;
        line-height: 50px;
      }
    }
  }
}
</style>
